package com.a0soft.gphone.aTruffleHog.wnd.stat;

import android.content.Context;
import android.os.Parcelable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;

import com.a0soft.gphone.aTruffleHog.util.aDebug;
import com.jakewharton.android.viewpagerindicator.TitlePageIndicator;
import com.jakewharton.android.viewpagerindicator.TitleProvider;

public abstract class BaseViewPager {
    protected static final int VIEW_PAGER_DEFAULT_ID = 0x1000;
    protected static final int VIEW_PAGER_OS_ID = VIEW_PAGER_DEFAULT_ID + 1;
    protected static final int VIEW_PAGER_ROOTED_ID = VIEW_PAGER_DEFAULT_ID + 2;
    protected static final int VIEW_PAGER_TOP_DEVICE_ID = VIEW_PAGER_DEFAULT_ID + 3;
    protected static final int VIEW_PAGER_TOP_OPERATOR_ID = VIEW_PAGER_DEFAULT_ID + 4;
    protected static final int VIEW_PAGER_FIND_BY_CAMERA_ID = VIEW_PAGER_DEFAULT_ID + 10;
    protected static final int VIEW_PAGER_FIND_BY_CPU_ID = VIEW_PAGER_DEFAULT_ID + 11;

    protected ViewPager         m_pager;
    private MyPagerAdapter      m_adapter;
    private TitlePageIndicator  m_indicator;
    
    private static final String LOG_TAG = BaseViewPager.class.getSimpleName();
    
    ////////////////////////////////////////
    protected abstract int GetViewPagerID();
    protected abstract String[] GetTitles(Context ctx);
    protected abstract Fragment GetPage(Context ctx, int nPos);
    
    public BaseViewPager()
    { 
    }
    
    public void 
    SetupViews(FragmentActivity act, 
               ViewPager pager, TitlePageIndicator indicator) 
    {
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "SetupViews()");

        String[] strTitles = GetTitles(act);
        
        m_adapter = new MyPagerAdapter(act, 
                                       act.getSupportFragmentManager(),
                                       strTitles);

        m_pager = pager;
        //need an unique ID for ViewPager as FragmentPagerAdapter will use the ID to search fragment
        m_pager.setId(GetViewPagerID());
        
        indicator.setVisibility(strTitles.length <= 1 ? View.GONE : View.VISIBLE);
        m_indicator = indicator;

        m_pager.setAdapter(m_adapter);
        m_indicator.setViewPager(m_pager);
        
        m_indicator.setCurrentItem(0);
    }
    
    public Fragment
    FindFragmentByPos(FragmentActivity act, int nPos)
    {
        String strTag = null;
        if (m_adapter != null)
            strTag = m_adapter.GetTag(nPos);
        if (aDebug.ENABLED) aDebug.d(LOG_TAG, "FindFragment(" + nPos + "): strTag=" + strTag);
        
        if (strTag != null) {
            FragmentManager mgr = act.getSupportFragmentManager();
            return mgr.findFragmentByTag(strTag);
        }   
        return null;
    }
    
    //////////////////////////////////////////////////////////
    private class MyPagerAdapter extends FragmentPagerAdapter 
                                        implements TitleProvider 
    {
        private String[] m_strTitles;
        private String[] m_strTags;
        
        MyPagerAdapter(Context ctx, FragmentManager fm, String[] strTitles)
        {
            super(fm);
            m_strTitles = strTitles;
            m_strTags = new String[strTitles.length];
        }
        
        public String
        GetTag(int nPos)
        {
            return m_strTags[nPos];
        }
        
        @Override public int 
        getCount()
        {
            return m_strTitles.length;
        }

        @Override public String 
        getTitle(int nPos)
        {
            return m_strTitles[nPos];
        }

        @Override public Fragment 
        getItem(int nPos) 
        {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "MyPagerAdapter::getItem(), nPos=" + nPos);
            return GetPage(m_pager.getContext(), nPos);
        }
        
        @Override public Parcelable 
        saveState()
        {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "MyPagerAdapter::saveState()");
            return super.saveState();
        }

        @Override public void 
        restoreState(Parcelable state, ClassLoader loader)
        {
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "MyPagerAdapter::restoreState()");
            super.restoreState(state, loader);
        }

        @Override public Object 
        instantiateItem(ViewGroup container, int nPos)
        {
            Fragment frg = (Fragment)super.instantiateItem(container, nPos);

            String strTag = frg.getTag();
            if (aDebug.ENABLED)
                aDebug.d(LOG_TAG, "MyPagerAdapter::instantiateItem(), nPos=" + nPos + ", strTag=" + strTag);
                        
            m_strTags[nPos] = strTag;
            
            return frg;
        }
    }
}
